// component/middlePage/index.js
var app = getApp();
let urlIp = app.globalData.urlIp;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  wxlogin() {
    let self = this;
    wx.login({
      success(res) {
        console.log(res, '登录的code')
        if (res.code) {

          //发起网络请求
          self.axios.noToken('WeChat/getAppletUnionId', { code: res.code }, "POST").then((result) => {
            if (result.code == '200') {
              let obj = {};
              obj.tenantId = result.data.openid;
              obj.username = '111';
              obj.password = '222';
              self.axios.addFix("Session", obj).then(res => {
                if (res.code == '200') {
                  wx.setStorageSync("userid", res.data.user.id); //设置userid
                  wx.setStorageSync("token", res.data.token);
                  self.axios.custom("User", "own").then(res => {
                    if (res.code == '200') {

                      if (res.data.path) {
                        let hadeUrl = urlIp + res.data.path;
                        wx.setStorageSync("headUrl", hadeUrl);
                      }
                      wx.setStorageSync("errorCount", 0);
                      wx.setStorageSync("power", res.data.funcPermissions);
                      wx.setStorageSync("username", res.data.username);
                      wx.setStorageSync("customerId", res.data.customerId);
                      wx.setStorageSync("customerName", res.data.customerName);
                      wx.setStorageSync("customerCode", res.data.customerCode);
                      wx.setStorageSync("identity", res.data.typeName);
                      wx.setStorageSync("personName", res.data.personName);
                      wx.setStorageSync("area", res.data.area);
                      wx.setStorageSync("station", res.data.station);
                      wx.setStorageSync("phone", res.data.phone);
                      wx.setStorageSync("bindWeChat", res.data.bindWeChat);
                      //wx.switchTab({ url: '/pages/main/index' });
                      wx.reLaunch({
                        url: '/pages/repair/submit/index',
                        success: (result) => {
                        },
                        fail: () => { },
                        complete: () => { }
                      });
                    }
                  })
                } else {
                  console.log(res, '点击确定后')
                  if (res.click) {
                    // 清理token
                    wx.reLaunch({
                      url: '/packageA/login/login',
                      success: (result) => {
                      },
                      fail: () => { },
                      complete: () => { }
                    });
                  }
                }
              })
            }
          })
        } else {

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 授权判定
    this.wxlogin();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})